#include "inputcndialog.h"
#include "ui_inputcndialog.h"
#include "delegate.h"

InputCnDialog::InputCnDialog(Experiment* pExperiment,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InputCnDialog)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;
    ComboxDelegate* pComboxDelegate=new ComboxDelegate(this,2,pExperiment);//
    ui->tableWidget_Quantitative->setItemDelegateForRow(0,pComboxDelegate);
    pComboxDelegate=new ComboxDelegate(this,2,pExperiment);
    ui->tableWidget_Standard->setItemDelegateForRow(0,pComboxDelegate);
    LoadData();

    connect(ui->pushButton_Cancel,SIGNAL(clicked()),this,SLOT(close()));
}

InputCnDialog::~InputCnDialog()
{
    delete ui;
}

void InputCnDialog::LoadData()
{
   // ExpMethod* pExpMethod=pExperiment->GetExpMethodPtr();

    QMap<double,QString> pairs=pExperiment->GetQuantitativeElement(2);
    QStringList header=QStringList()<<"样品序号"<<"样品名称";
    QList<QString> elements=pairs.values();
    header.append(elements);
    ui->tableWidget_Quantitative->clear();
    ui->tableWidget_Quantitative->setColumnCount(header.count());
    ui->tableWidget_Quantitative->setHorizontalHeaderLabels(header);

    ExpSample* pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();

    int row=0;
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        if(sampleInfor.sampleType=="全定量标准")//"标准加入标准"
        {
            QTableWidgetItem *pItem;

            if(row==0)//add
            {
                ui->tableWidget_Quantitative->insertRow(ui->tableWidget_Quantitative->rowCount());
                int column=0;
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
                pItem->setText("");//
                ui->tableWidget_Quantitative->setItem(row,column++,pItem);
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
                pItem->setText("");//
                ui->tableWidget_Quantitative->setItem(row,column++,pItem);
                for(int j=0;j<elements.count();j++)
                {
                    pItem=new QTableWidgetItem;
                    pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);//
                    pItem->setText("ppb");//
                    ui->tableWidget_Quantitative->setItem(row,column++,pItem);
                }
                row++;
            }

            ui->tableWidget_Quantitative->insertRow(ui->tableWidget_Quantitative->rowCount());
            int column=0;
            pItem=new QTableWidgetItem;
            pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
            pItem->setText(QString::number(i));//
            ui->tableWidget_Quantitative->setItem(row,column++,pItem);
            pItem=new QTableWidgetItem;
            pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_Quantitative->setItem(row,column++,pItem);

            for(int j=0;j<elements.count();j++)
            {
                QString name=elements.at(j);
                QHash<QString,double> datas=sampleInfor.GetCnInputs();
                if(datas.contains(name)==false)
                {
                    datas.insert(name,0);//
                }
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);//
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_Quantitative->setItem(row,column++,pItem);
            }
            row++;
        }
    }


    pairs=pExperiment->GetQuantitativeElement(3);
    header=QStringList()<<"样品序号"<<"样品名称";
    elements=pairs.values();
    header.append(elements);
    ui->tableWidget_Standard->clear();
    ui->tableWidget_Standard->setColumnCount(header.count());
    ui->tableWidget_Standard->setHorizontalHeaderLabels(header);

    row=0;
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        if(sampleInfor.sampleType=="标准加入标准")
        {
            QTableWidgetItem *pItem;

            if(row==0)//
            {
                ui->tableWidget_Standard->insertRow(ui->tableWidget_Standard->rowCount());
                int column=0;
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
                pItem->setText("");//
                ui->tableWidget_Standard->setItem(row,column++,pItem);
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
                pItem->setText("");//
                ui->tableWidget_Standard->setItem(row,column++,pItem);
                for(int j=0;j<elements.count();j++)
                {
                    pItem=new QTableWidgetItem;
                    pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);//
                    pItem->setText("ppb");//
                    ui->tableWidget_Standard->setItem(row,column++,pItem);
                }
                row++;
            }

            ui->tableWidget_Standard->insertRow(ui->tableWidget_Standard->rowCount());
            int column=0;
            pItem=new QTableWidgetItem;
            pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
            pItem->setText(QString::number(i));//
            ui->tableWidget_Standard->setItem(row,column++,pItem);
            pItem=new QTableWidgetItem;
            pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);//
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_Standard->setItem(row,column++,pItem);

            for(int j=0;j<elements.count();j++)
            {
                QString name=elements.at(j);
                QHash<QString,double> datas=sampleInfor.GetCnInputStandards();
                if(datas.contains(name)==false)
                {
                    datas.insert(name,0);//
                }
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);//
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_Standard->setItem(row,column++,pItem);
            }
            row++;
        }
    }
}

void InputCnDialog::on_pushButton_Ok_clicked()
{
    int row,column;
    SampleInfor* pSampleInfor;
    QAbstractItemModel * pModel;
    ExpSample* pExpSample=pExperiment->GetExpSamplePtr();

    row=ui->tableWidget_Quantitative->rowCount();
    column=ui->tableWidget_Quantitative->columnCount();
    pModel=ui->tableWidget_Quantitative->model();
    for(int i=0;i<row;i++)
    {
        QHash<QString,double> cns;
        QTableWidgetItem *pItem;
        if(i==0) continue;//
        for(int j=2;j<column;j++)
        {
           QString name=pModel->headerData(j,Qt::Horizontal).toString();//
           qDebug()<<"name"<<name;
           pItem=ui->tableWidget_Quantitative->item(i,j);
           double data=pItem->text().toDouble();
           cns.insert(name,data);
        }

        pItem=ui->tableWidget_Quantitative->item(i,0);
        int index=pItem->text().toInt();
        pSampleInfor=pExpSample->GetSampleInforPtr(index);
        pSampleInfor->SetCnInputs(cns);
        qDebug()<<"cns"<<cns<<pSampleInfor->GetCnInputs()<<row<<column;
    }

    row=ui->tableWidget_Standard->rowCount();
    column=ui->tableWidget_Standard->columnCount();
    pModel=ui->tableWidget_Standard->model();
    for(int i=0;i<row;i++)
    {
        QHash<QString,double> cns;
        QTableWidgetItem *pItem;
        if(i==0) continue;//
        for(int j=2;j<column;j++)
        {
           QString name=pModel->headerData(j,Qt::Horizontal).toString();//
           qDebug()<<"name"<<name;
           pItem=ui->tableWidget_Standard->item(i,j);
           double data=pItem->text().toDouble();
           cns.insert(name,data);
        }

        pItem=ui->tableWidget_Standard->item(i,0);
        int index=pItem->text().toInt();
        pSampleInfor=pExpSample->GetSampleInforPtr(index);
        pSampleInfor->SetCnInputStandards(cns);
        qDebug()<<"cns"<<cns<<pSampleInfor->GetCnInputStandards()<<row<<column;
    }

    this->close();
}
